/*
 * parser-tools.c
 *
 *  Created on: Apr 26, 2013
 *      Author: koldar
 */

#include "parser-tools.h"
#include <stdlib.h>

void parseerror(int tokenexpected){
	fprintf(stderr,"parse error at line %d; expected \" %d",yylineno,tokenexpected);
	fprintf(stderr," \"\n");
	fprintf(stderr,"%s\n",yytext);
	exit(EXIT_FAILURE);
}

void next(){
	lookahead=yylex();
}

LexVal match(int token){
	LexVal result=lexval;
	if (lookahead==token){
		next();
		return result;
	}else{
		parseerror(token);
		return result;
	}
}

//TODO
char* truncateQuotes(char* str){
	char* result=(char*)malloc(sizeof(char)*(strlen(str)-2));
	checkMalloc(result,"parser-tools.c:truncateQuotes:1");
	char* initialresult=result;
	str++;
	while (*str!='\0'&&!(*str=='\"'&&(*(str+sizeof(char))=='\0'))){
		*result++=*str++;
	}
	return initialresult;
}
